#include <QString>
#include "oxfordlasersensordll.h"
#include "package/simpletcpsocketdll_global.h"
extern "C" OXFORDLASERSENSORDLLSHARED_EXPORT OxfordLaserSensorDLL* GetOxfordLaserSensorInstance(){
    return new OxfordLaserSensor();
}

OxfordLaserSensor::OxfordLaserSensor(){
    this->mLaserSensorData.SensorConnectMark = false;
}
OxfordLaserSensor::~OxfordLaserSensor(){}

short OxfordLaserSensor::LaserSensorConnect(const QString ip){
    COM_addTcpClient(ip,5020,this->toLaserSensorSocket);
    COM_connectToServer(this->toLaserSensorSocket);
    this->mLaserSensorData.SensorConnectMark = true;
    return 1;
}

short OxfordLaserSensor::LaserSensorLoopRun(){
    if(this->mLaserSensorData.SensorConnectMark == true){
        if(!COM_getClientSendStatus(this->toLaserSensorSocket)){
            if(COM_getClientReadStatus(this->toLaserSensorSocket)){
                COM_clientReadData(this->toLaserSensorSocket,RecvRawLaserSensorData);
                this->LaserSensorDataDeal();
            }
            const char Order[10] = { 0x01, 0x08, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
            COM_clientSendData(this->toLaserSensorSocket,Order);
        }
        return 1;
    }
    else{
        return 0;
    }
}

short OxfordLaserSensor::LaserSensorLoopRunSend(){
    if(this->mLaserSensorData.SensorConnectMark == true){
        if(!COM_getClientSendStatus(this->toLaserSensorSocket)){
            const char Order[10] = { 0x01, 0x08, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
            COM_clientSendData(this->toLaserSensorSocket,Order);
        }
        return 1;
    }
    else {
        return 0;
    }
}

short OxfordLaserSensor::LaserSensorLoopRunReceive(){
    if(this->mLaserSensorData.SensorConnectMark == true){
        if(COM_getClientReadStatus(this->toLaserSensorSocket)){
            COM_clientReadData(this->toLaserSensorSocket,RecvRawLaserSensorData);
            this->LaserSensorDataDeal();
        }
        return 1;
    }
    else{
        return 0;
    }
}

short OxfordLaserSensor::LaserSensorDataDeal(){
    unsigned char H = 0;
    unsigned char L = 0;

    signed short X = 0;    signed short Y = 0;    signed short Z = 0;
    signed short A = 0;    signed short B = 0;    signed short C = 0;
    signed short Gap = 0;  signed short Mismatch=0;

    H = RecvRawLaserSensorData[0];
    L = RecvRawLaserSensorData[1];
    this->mLaserSensorData.ErrorMessage = L;

    H = RecvRawLaserSensorData[2];
    L = RecvRawLaserSensorData[3];
    X = (H << 8) | L;
    mLaserSensorData.Data[0] = X;
    mLaserSensorData.Data[0] = mLaserSensorData.Data[0] / 100;
    this->mLaserSensorData.X = mLaserSensorData.Data[0];

    H = RecvRawLaserSensorData[4];
    L = RecvRawLaserSensorData[5];
    Y = (H << 8) | L;
    mLaserSensorData.Data[1] = Y;
    mLaserSensorData.Data[1] = mLaserSensorData.Data[1] / 100;
    this->mLaserSensorData.Y = mLaserSensorData.Data[1];

    H = RecvRawLaserSensorData[6];
    L = RecvRawLaserSensorData[7];
    Z = (H << 8) | L;
    mLaserSensorData.Data[2] = Z;
    mLaserSensorData.Data[2] = mLaserSensorData.Data[2] / 100;
    this->mLaserSensorData.Z = mLaserSensorData.Data[2];

    H = RecvRawLaserSensorData[8];
    L = RecvRawLaserSensorData[9];
    Gap = (H << 8) | L;
    mLaserSensorData.Data[3] = Gap;
    mLaserSensorData.Data[3] = mLaserSensorData.Data[3] / 100;
    this->mLaserSensorData.Gap = mLaserSensorData.Data[3];

    H = RecvRawLaserSensorData[10];
    L = RecvRawLaserSensorData[11];
    Mismatch = (H << 8) | L;
    mLaserSensorData.Data[4] = Mismatch;
    mLaserSensorData.Data[4] = mLaserSensorData.Data[4] / 100;
    this->mLaserSensorData.Mismatch = mLaserSensorData.Data[4];

    H = RecvRawLaserSensorData[12];
    L = RecvRawLaserSensorData[13];
    A = (H << 8) | L;
    mLaserSensorData.Data[5] = A;
    mLaserSensorData.Data[5] = mLaserSensorData.Data[5] / 100;
    this->mLaserSensorData.A = mLaserSensorData.Data[5];

    H = RecvRawLaserSensorData[14];
    L = RecvRawLaserSensorData[15];
    B = (H << 8) | L;
    mLaserSensorData.Data[6] = B;
    mLaserSensorData.Data[6] = mLaserSensorData.Data[6] / 100;
    this->mLaserSensorData.B = mLaserSensorData.Data[6];

    H = RecvRawLaserSensorData[16];
    L = RecvRawLaserSensorData[17];
    C = (H << 8) | L;
    mLaserSensorData.Data[7] = C;
    mLaserSensorData.Data[7] = mLaserSensorData.Data[7] / 100;
    this->mLaserSensorData.C = mLaserSensorData.Data[7];
    return 1;
}

short OxfordLaserSensor::isError(){
    return this->mLaserSensorData.ErrorMessage;
}

short OxfordLaserSensor::GetXDataOnLaserCoordinate(double &data){
    data = this->mLaserSensorData.X;
    return 1;
}
short OxfordLaserSensor::GetYDataOnLaserCoordinate(double &data){
    data = this->mLaserSensorData.Y;
    return 1;
}
short OxfordLaserSensor::GetZDataOnLaserCoordinate(double &data){
    data = this->mLaserSensorData.Z;
    return 1;
}
short OxfordLaserSensor::GetADataOnLaserCoordinate(double &data){
    data = this->mLaserSensorData.A;
    return 1;
}
short OxfordLaserSensor::GetBDataOnLaserCoordinate(double &data){
    data = this->mLaserSensorData.B;
    return 1;
}
short OxfordLaserSensor::GetCDataOnLaserCoordinate(double &data){
    data = this->mLaserSensorData.C;
    return 1;
}
short OxfordLaserSensor::GetGapData(double &data){
    data = this->mLaserSensorData.Gap;
    return 1;
}
short OxfordLaserSensor::GetMismatchData(double &data){
    data = this->mLaserSensorData.Mismatch;
    return 1;
}
